package com.selfmade.transferutil.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.selfmade.transferutil.R;
import com.selfmade.transferutil.Transition;
import com.selfmade.transferutil.app.MyApp;
import com.selfmade.transferutil.model.Data;


public class MainActivity extends AppCompatActivity {

    private Spinner spinner;
    private Button transfer, queryHistory, queryBook;
    private EditText money;
    private TextView balance, currentUser;
    private ArrayAdapter<String> arrayAdapter;
    private String receiver  = null;
    private Handler handler;
    private Data receiveMoney;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        spinner = (Spinner) findViewById(R.id.userList);
        transfer = (Button) findViewById(R.id.transfer);
        money = (EditText) findViewById(R.id.money);
        balance = (TextView) findViewById(R.id.balance);
        balance.setText(Integer.toString(MyApp.transition.getBalance()));
        currentUser = (TextView) findViewById(R.id.currentUser);
        currentUser.setText(MyApp.transition.currentUser);
        queryHistory = (Button) findViewById(R.id.queryHistory);
        queryBook = (Button) findViewById(R.id.queryBook);

        MyApp.userList.remove(Transition.currentUser);
        System.out.println("MainActivity" + Transition.currentUser);

        arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, MyApp.userList);
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(arrayAdapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                System.out.println("点击的是" + MyApp.userList.get(position));
                receiver = MyApp.userList.get(position);
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        transfer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!money.getText().toString().equals("")) {
                    MyApp.transition.expend(receiver, Integer.parseInt(money.getText().toString()));
                    Toast.makeText(MainActivity.this, "转出成功！", Toast.LENGTH_LONG).show();
                    balance.setText(Integer.toString(MyApp.transition.getBalance()));
                } else {
                    Toast.makeText(MainActivity.this, "请输入转账金额！", Toast.LENGTH_LONG).show();
                }
            }
        });
        queryHistory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println(MyApp.transition.queryHistory());
                startActivity(new Intent(MainActivity.this, DataListActivity.class));
            }
        });
        queryBook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, BookActivity.class));
            }
        });

        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 1) {
                    balance.setText(Integer.toString(MyApp.transition.getBalance()));
                    Toast.makeText(MainActivity.this, "收到" + receiveMoney.getTransfer() + "的转账：" + receiveMoney.getMoney() + "元！", Toast.LENGTH_LONG).show();
                }
            }
        };
        MyApp.transition.setCallBack(new Transition.UpdateBalance() {
            @Override
            public void notifyUpdate(Data receiveMoney) {
                if (receiveMoney != null) {
                    System.out.println("receiveMoney:" + receiveMoney.toString());
                    MainActivity.this.receiveMoney = receiveMoney;
                    handler.sendEmptyMessage(1);
                }
            }
        });
    }


}
